package com.example.spring.controller;


import com.example.spring.entity.Good;
import com.example.spring.entity.Slot;
import com.example.spring.mapper.SlotMapper;
import com.example.spring.service.SlotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/slot")
public class SlotController {

    @Autowired
    private SlotMapper slotMapper;

    @Autowired
    private SlotService slotService;

    //新增或修改
    @PostMapping()
    public Integer save(@RequestBody Slot slot) {
        //新增或者更新
        return slotService.save(slot);
    }
    //查询所有数据
    @GetMapping()
    public List<Slot> findAll() {
        List<Slot> all =  slotMapper.findAll();
        return all;
    }
    //删除
    @DeleteMapping("/{sid}")
    public Integer delete(@PathVariable Integer sid)
    {
        return slotMapper.deleteById(sid);
    }

    //给定whid查询slot及货物数量
    @PostMapping("/select")
    public List<Object> find_slot_by_whid(@RequestParam Integer whid){
        List<Integer> total = slotMapper.findByWhidSlot(whid);
        List<Integer> slot = slotMapper.findWhSlot(whid);
        List<Object> res = new ArrayList<Object>();
        for (int i = 0; i < total.size(); i++)
        {
            Map<String, Object> res1 = new HashMap<>();
            res1.put("slot", slot.get(i));
            res1.put("total", total.get(i));
            res.add(res1);
        }
        return res;
    }

    //给定slot查good及数量
    @PostMapping("select/slot/Page")
    public Map<String, Object> find_good_by_sid(@RequestParam Integer sid,
                                                 @RequestParam Integer pageNum,
                                                 @RequestParam Integer pageSize){
        pageNum = (pageNum - 1) * pageSize;
        List<Good> data =  slotMapper.findBySlotId(sid, pageNum, pageSize);
        Integer total = slotMapper.findBySlotId_Total(sid);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }

}
